{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Distribute Candies Among Children II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #combinatorics #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #组合数学 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: distributeCandies"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #给小朋友们分糖果 II"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个正整数&nbsp;<code>n</code> 和&nbsp;<code>limit</code>&nbsp;。</p>\n",
    "\n",
    "<p>请你将 <code>n</code>&nbsp;颗糖果分给 <code>3</code>&nbsp;位小朋友，确保没有任何小朋友得到超过 <code>limit</code>&nbsp;颗糖果，请你返回满足此条件下的&nbsp;<strong>总方案数</strong>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong class=\"example\">示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>n = 5, limit = 2\n",
    "<b>输出：</b>3\n",
    "<b>解释：</b>总共有 3 种方法分配 5 颗糖果，且每位小朋友的糖果数不超过 2 ：(1, 2, 2) ，(2, 1, 2) 和 (2, 2, 1) 。\n",
    "</pre>\n",
    "\n",
    "<p><strong class=\"example\">示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>n = 3, limit = 3\n",
    "<b>输出：</b>10\n",
    "<b>解释：</b>总共有 10 种方法分配 3 颗糖果，且每位小朋友的糖果数不超过 3 ：(0, 0, 3) ，(0, 1, 2) ，(0, 2, 1) ，(0, 3, 0) ，(1, 0, 2) ，(1, 1, 1) ，(1, 2, 0) ，(2, 0, 1) ，(2, 1, 0) 和 (3, 0, 0) 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10<sup>6</sup></code></li>\n",
    "\t<li><code>1 &lt;= limit &lt;= 10<sup>6</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [distribute-candies-among-children-ii](https://leetcode.cn/problems/distribute-candies-among-children-ii/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [distribute-candies-among-children-ii](https://leetcode.cn/problems/distribute-candies-among-children-ii/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['5\\n2', '3\\n3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        if limit > n:\n",
    "            return math.comb(n+2, 2)\n",
    "        \n",
    "        @cache\n",
    "        def dp(i, k):\n",
    "            if k == 0:\n",
    "                return min(i, limit) - max(0, i - limit) + 1\n",
    "            \n",
    "            ans = 0\n",
    "            for j in reversed(range(limit+1)):\n",
    "                if i - j > 2 * limit: break\n",
    "                ans += dp(i - j, k - 1)\n",
    "            return ans\n",
    "        \n",
    "        return dp(n, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(n+1):\n",
    "            for j in range(n+1):\n",
    "                k = n - i - j\n",
    "                if k >= 0 and i <= limit and j <= limit and k <= limit:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        ans = 0\n",
    "        for i in range(limit+1):\n",
    "            for j in range(limit+1):\n",
    "                for k in range(limit+1):\n",
    "                    if i+j+k == n:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\r\n",
    "        return self.solve2(n, limit)\r\n",
    "    \"\"\"\r\n",
    "        1. Brute Force\r\n",
    "    \"\"\"\r\n",
    "    def solve1(self, n: int, limit: int) -> int:\r\n",
    "        ans = 0\r\n",
    "        for i in range(limit+1):\r\n",
    "            for j in range(limit+1):\r\n",
    "                k = n - i - j\r\n",
    "                if k >= 0 and k <= limit:\r\n",
    "                    ans += 1\r\n",
    "        return ans\r\n",
    "    \"\"\"\r\n",
    "        2. 排容原理\r\n",
    "        a. 所有方案數：\r\n",
    "            H(3, n) = C(n+2, n) = C(n+2, 2)\r\n",
    "        b. 至少一人拿到超過 limit 個糖果的方案數：\r\n",
    "            (先分(limit+1)給他，剩下的分給所有人)\r\n",
    "            3 * H(3, n-(limit+1)) = 3 * C(n−(limit+1)+2, 2)\r\n",
    "        c. 至少兩人拿到超過 limit 個糖果的方案數：\r\n",
    "            3 * C(n−2⋅(limit+1)+2, 2)\r\n",
    "    \"\"\"\r\n",
    "\r\n",
    "    def solve2(self, n: int, limit: int) -> int:\r\n",
    "        if n > 3 * limit: # 三人都拿到超過 limit 個糖果，沒有方案\r\n",
    "            return 0\r\n",
    "        ans = comb(n + 2, 2)\r\n",
    "        if n >= (limit + 1): # 至少一人拿到超過 limit 個糖果\r\n",
    "            ans -= 3 * comb(n - limit + 1, 2)\r\n",
    "        if n >= 2 * (limit + 1): # 至少兩人拿到超過 limit 個糖果\r\n",
    "            ans += 3 * comb(n - 2 * limit, 2)\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        @cache\n",
    "        def f(i,n):\n",
    "            if i==2 or n<0:\n",
    "                if n>=0 and n<=limit:\n",
    "                    return 1\n",
    "                return 0\n",
    "            m=0\n",
    "            for j in range(limit+1):\n",
    "                m+=f(i+1,n-j)\n",
    "            return m\n",
    "        return f(0,n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        \n",
    "        ans = 0\n",
    "        for i in range(0, min(n, limit)+1):\n",
    "            ans += max(0, min(limit, n - i) - max(0, n - i - limit) + 1)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "def c2(n):\n",
    "    return n * (n - 1) // 2 if n > 1 else 0\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        return c2(n + 2) - 3 * c2(n - limit + 1) + 3 * c2(n - 2 * limit) - c2(n - 3 * limit - 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\r\n",
    "        return self.solve2(n, limit)\r\n",
    "    \"\"\"\r\n",
    "        1. Brute Force\r\n",
    "    \"\"\"\r\n",
    "    def solve1(self, n: int, limit: int) -> int:\r\n",
    "        ans = 0\r\n",
    "        for i in range(limit+1):\r\n",
    "            for j in range(limit+1):\r\n",
    "                k = n - i - j\r\n",
    "                if k >= 0 and k <= limit:\r\n",
    "                    ans += 1\r\n",
    "        return ans\r\n",
    "    \"\"\"\r\n",
    "        2. 排容原理\r\n",
    "        a. 所有方案數：\r\n",
    "            H(3, n) = C(n+2, n) = C(n+2, 2)\r\n",
    "        b. 至少一人拿到超過 limit 個糖果的方案數：\r\n",
    "            (先分(limit+1)給他，剩下的分給所有人)\r\n",
    "            3 * H(3, n-(limit+1)) = 3 * C(n−(limit+1)+2, 2)\r\n",
    "        c. 至少兩人拿到超過 limit 個糖果的方案數：\r\n",
    "            3 * C(n−2⋅(limit+1)+2, 2)\r\n",
    "    \"\"\"\r\n",
    "\r\n",
    "    def solve2(self, n: int, limit: int) -> int:\r\n",
    "        if n > 3 * limit: # 三人都拿到超過 limit 個糖果，沒有方案\r\n",
    "            return 0\r\n",
    "        ans = comb(n + 2, 2)\r\n",
    "        if n >= (limit + 1): # 至少一人拿到超過 limit 個糖果\r\n",
    "            ans -= 3 * comb(n - limit + 1, 2)\r\n",
    "        if n >= 2 * (limit + 1): # 至少兩人拿到超過 limit 個糖果\r\n",
    "            ans += 3 * comb(n - 2 * limit, 2)\r\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        dp = list()\n",
    "        for i in range(min(n + 1, limit + 1)):\n",
    "            dp.append(i + 1)\n",
    "        x = limit + 1\n",
    "        while n >= x and x <= 2 * limit:\n",
    "            dp.append(2 * limit - x + 1)\n",
    "            x += 1\n",
    "        ans = 0\n",
    "        # print(dp)\n",
    "        for i in range(min(n + 1, limit + 1)):\n",
    "            if n <= 2 * limit + i:\n",
    "                ans += dp[n - i]\n",
    "        return ans \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        if 3 * limit < n:\n",
    "            return 0\n",
    "        cnt = [0] * (n + 1)\n",
    "        for i in range(min(n, 2 * limit) + 1):\n",
    "            if i > limit:\n",
    "                cnt[i] = 2 * limit - i + 1\n",
    "            else:\n",
    "                cnt[i] = i + 1\n",
    "        return sum(cnt[i] for i in range(n + 1) if n - i <= limit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def distributeCandies(self, n: int, limit: int) -> int:\n",
    "        \n",
    "#         @lru_cache(None)\n",
    "#         def dfs(i,j):\n",
    "#             if (3 - i) * limit < j:return 0\n",
    "#             if j == 0:return 1\n",
    "#             if i == 1:return max(0,min(limit,j) - max(0,(j - limit)) + 1)\n",
    "#             if i == 2:return j <= limit\n",
    "#             ans = 0\n",
    "#             for k in range(limit+1):\n",
    "#                 if k > j:break\n",
    "#                 ans += dfs(i+1,j-k)\n",
    "#             return ans\n",
    "        \n",
    "#         return dfs(0,n)\n",
    "    \n",
    "        dp = [[0 for _ in range(n+2)] for _ in range(5)]\n",
    "        for i in range(5):\n",
    "            dp[i][0] = 1\n",
    "        ans = 0\n",
    "        for j in range(max(0,n-limit),n+1):\n",
    "            if 2 * limit < j:\n",
    "                dp[1][j] = 0\n",
    "                break\n",
    "            # dp[1][j] = max(0,min(limit,j) - max(0,(j-limit)) + 1)\n",
    "            ans += max(0,min(limit,j) - max(0,(j-limit)) + 1)\n",
    "        return ans\n",
    "\n",
    "        # ans = 0\n",
    "        # if n <= limit:\n",
    "        #     for k in range(n+1):\n",
    "        #         ans += dp[1][n-k]\n",
    "        # else:\n",
    "        #     for k in range(limit+1):\n",
    "        #         ans += dp[1][n-k]\n",
    "        # return ans     "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
